<?php 
$builder = $this->maintenance->builder;
$builder->db = $this->db;
$db = $this->db;

InputBuilder::$singleton = $builder;

if (!isEmpty(getParam('operation'))) {
    switch (getParam('operation')) {
        case 's':
            save($db);
            break;
        case 'u':
            update($db);
            break;
        case 'd':
            delete($db);
            break;
        case 'r':
            $go = false;
            relacionar($db);
            break;
        case 'dr':
            $go = false;
            delRel($db);
            break;
    }
}

function delRel($db) {
    $s = getParam("SEL");
    if (isset($_GET["SESSION_ID"]) && $_GET["SESSION_ID"] != 0) {
        $db->query_delete("cllg_student_benefactor_tmp", "STUDENT IN ($s)");
        $arrExplode = explode(",", $s);
        if (count($arrExplode) > 0) {
            reset($arrExplode);
            while ($arrTMP = each($arrExplode)) {
                unset($_SESSION["BENEFACTORES"]["STUDENT"][$arrTMP["value"]]);
            }
        }
    } else {
        $db->query_delete("cllg_student_benefactor", "STUDENT IN ($s)");
    }
}

function relacionar($db) {

    if (isset($_GET["SESSION_ID"]) && $_GET["SESSION_ID"] != 0) {
        $data = array('STUDENT' => sqlValue(getParam('STUDENT'), 'int'),
            'BENEFACTOR' => sqlValue(getParam('SESSION_ID'), 'text'));
        $db->query_insert("cllg_student_benefactor_tmp", $data);
        $_SESSION["BENEFACTORES"]["STUDENT"][getParam('STUDENT')] = getParam('STUDENT');
    } else {
        $data = array('STUDENT' => sqlValue(getParam('STUDENT'), 'int'),
            'BENEFACTOR' => sqlValue(getParam('PARENT'), 'int'));
        $db->query_insert("cllg_student_benefactor", $data);
    }
}

function save($db) {
    $data = array('ID' => sqlValue(getParam('IDN'), 'text'),
        'RAZON_SOCIAL' => sqlValue(getParam('RAZON_SOCIAL'), 'text'),
        'TELEFONO' => sqlValue(getParam('TELEFONO'), 'text'),
        'DIRECCION' => sqlValue(getParam('DIRECCION'), 'text'),
        'FAX' => sqlValue(getParam('FAX'), 'text'),
        'EMAIL' => sqlValue(getParam('EMAIL'), 'text'),
        'STATUS' => sqlValue(getParam('STATUS'), 'text'));
    $db->query_insert('cllg_benefactor', $data);
    $rs = $db->query_toArray("select max(ID) mx from cllg_benefactor");
    $rs = $rs[0];
    $intParent = sqlValue($rs["mx"], 'text');

   // $builder = InputBuilder::$singleton;
   // $builder->saveFields(getParam('ID'));
    
    $_SESSION["mensaje"] = "¡Datos Guardados!";
}

function update($db) {
    $data = array('ID' => sqlValue(getParam('IDB'), 'text'),
        'RAZON_SOCIAL' => sqlValue(getParam('RAZON_SOCIAL'), 'text'),
        'TELEFONO' => sqlValue(getParam('TELEFONO'), 'text'),
        'DIRECCION' => sqlValue(getParam('DIRECCION'), 'text'),
        'FAX' => sqlValue(getParam('FAX'), 'text'),
        'EMAIL' => sqlValue(getParam('EMAIL'), 'text'));
    $db->query_update('cllg_benefactor', $data, 'ID=' . sqlValue(getParam('IDB'), 'int'));

   // $builder = InputBuilder::$singleton;
   // $builder->saveFields(getParam('ID'));
    
    $_SESSION["mensaje"] = "¡Datos Guardados!";
}

function delete($db) {
    $db->query_delete('cllg_benefactor', 'ID=' . sqlValue(getParam('ID'), 'text'));
    $fields = &unserialize($_SESSION['fields']); //campos dinamicos a borrar
    foreach ($fields as $fld) {
        $db->query_delete('cllg_table_valuebyfield',
                'FK_TABLEFIELD=' . sqlValue($fld, 'text')
                . ' AND PK_RECORD=' . sqlValue(getParam('ID'), 'text'));
    }
}

?>